<template>
  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input v-model="data.filmName" placeholder="请输入电影名称查询" style="width: 300px; margin-right: 10px"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="filmName" label="电影名称" show-overflow-tooltip="true" />
        <el-table-column prop="comment" label="评论">
          <template #default="scope">
            <el-button @click="preview(scope.row.comment)" class="longComment">查看评论</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="用户名称" />
        <el-table-column prop="time" sortable label="评论时间" />
        <el-table-column prop="type" label="类型">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.type === '短评'">短评</el-tag>
            <el-tag type="success" v-if="scope.row.type === '长评'">长评</el-tag>
            <el-tag type="info" v-if="scope.row.type === '回复'">回复</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="160">
          <template #default="scope">
            <el-button round plain type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="card">
      <el-pagination background layout="->, total, prev, pager, next" v-model:current-page="data.pageNum"
                     v-model:page-size="data.pageSize" :total="data.total" @current-change="load" />
    </div>

    <!-- 评论预览 -->
    <el-dialog v-model="data.formVisible" title="评论内容" width="40%">
      <div class="longComment" v-html="data.commentContent" />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
  tableData: [],
  total: 0,
  pageNum: 1,
  pageSize: 10,
  filmName: null,
  commentContent: null,
  formVisible: false
})

const load = () => {
  request.get('/seriesComment/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      filmName: data.filmName,
      userId: data.user.role === 'ADMIN' ? null : data.user.id
    }
  }).then(res => {
    data.tableData = res.data.list
    data.total = res.data.total
  })
}
load()

// 查看评论
const preview = (comment) => {
  data.commentContent = comment
  data.formVisible = true
}

const reset = () => {
  data.filmName = null
  load()
}

// 删除
const del = (id) => {
  ElMessageBox.confirm('删除数据后无法恢复，确认删除吗？', '确认删除', { type: 'warning' }).then(res => {
    request.delete('/seriesComment/delete/'+id).then(res=>{
      if(res.code === '200'){
        load()
        ElMessage.success('操作成功')
      }else{
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {})
}

/**
 * 批量删除
 * @param rows
 */
const handleSelectionChange = (rows) => {
  data.ids = rows.map(row => row.id)
}
const delBatch = () => {
  if(!data.ids){
    ElMessage.error('请选择要删除的数据')
  }else{
    ElMessageBox.confirm('删除后无法恢复，确定要删除吗？', '确认删除', {type: "warning"}).then(res => {
      request.delete('/seriesComment/delete-batch' , {data:data.ids}).then(res => {
        if (res.code === '200') {
          load()
          ElMessage.success('操作成功')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
    })
  }
}
</script>
